import { topCategory } from '@/api/constants'
import { findAllCategory } from '@/api/category'

const state = () => {
  return {
    list: topCategory.map(item => ({ name: item }))
  }
}
const mutations = {
  setList (state, data) {
    state.list = data
  },
  // 修改当前一级分类下的open数据为true
  show (state, item) {
    const category = state.list.find(category => category.id === item.id)
    category.open = true
  },
  // 修改当前一级分类下的open数据为false
  hide (state, item) {
    const category = state.list.find(category => category.id === item.id)
    category.open = false
  }
}
const actions = {
  async getList (store) {
    const { result } = await findAllCategory()
    result.forEach(item => {
      item.open = false
    })
    // 获取数据成功，提交mutations进行数据修改
    store.commit('setList', result)
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
